from flask import Blueprint, render_template, request, g, redirect,url_for
from exts import db
from blueprints.forms import QuestionForm, AnswerForm
from models.Answer_model import AnswerModel
from models.question_model import QuestionModel
from decorators import login_required


bp = Blueprint('qa', __name__, url_prefix='/')

@bp.route('/')
def index():
    questions = QuestionModel.query.order_by(QuestionModel.create_time.desc()).all()
    return render_template("index.html",questions=questions)

@bp.route('/qa/public', methods=['GET', 'POST'])
@login_required
def public():
    if request.method == 'GET':
        return render_template("public_question.html")
    else:
        form = QuestionForm(request.form)
        if form.validate():
            title = form.title.data
            content = form.content.data
            question = QuestionModel(title=title, content=content, author=g.user)
            db.session.add(question)
            db.session.commit()
            return redirect("/")
        else:
            print(form.errors)
            return redirect(url_for("qa.public"))


@bp.route('/qa/detail/<qa_id>', methods=['GET'])
def qa_detail(qa_id):
    question = QuestionModel.query.get(qa_id)
    return render_template("detail.html", question=question)

# @bp.route("/answer/public",methods=['POST'])
@bp.post('/answer/public')
@login_required
def public_answer():
    form = AnswerForm(request.form)
    if form.validate():
        content = form.content.data
        question_id = form.question_id.data
        answer = AnswerModel(content=content, question_id=question_id,author_id=g.user.id)
        db.session.add(answer)
        db.session.commit()
        return redirect(url_for("qa.qa_detail",qa_id=question_id))
    else:
        print(form.errors)
        redirect(url_for("qa.qa_detail",qa_id=request.form.get("question_id")))


@bp.route('/search', methods=['GET'])
def search():
    # search?=xxx
    # search/<xxxxx>
    # post, request.form
    q = request.args.get('q')
    if q is None:
        questions = QuestionModel.query.all()
        return render_template("index.html", questions=questions)
    else:
        questions = QuestionModel.query.filter(QuestionModel.title.contains(q)).all()
        return render_template("index.html", questions=questions)


# 总结
# url 传参
# 邮件发送
# ajax
# cookie
# jinja模板